//
//  DNReSetPWDVC.swift
//  LunalerApp
//
//  Created by 露乐ios on 2018/3/13.
//  Copyright © 2018年 露乐ios. All rights reserved.
//

import UIKit
import SolarNetwork
class DNReSetPWDVC: DNBaseViewController {

    
    @IBOutlet weak var oldPwdTF: UITextField!
    
    @IBOutlet weak var newPwdTF: UITextField!
    
    @IBOutlet weak var confirmBtn: DNInspectButton!
    
    var viewModel : DNReSetPWDViewModel = {
        let viewModel = DNReSetPWDViewModel().initWithDelegate(delegate: self as AnyObject)
        return viewModel
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func initUI() {
        self.title = "修改密码"
        NotificationCenter.default.addObserver(self, selector: #selector(DNReSetPWDVC.textFieldDidChange(noti:)), name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
    }
    
    @objc func textFieldDidChange(noti:NSNotification) -> Void {
        updateLoginBtnState()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: false)
    }
    
    func updateLoginBtnState() -> Void {
        self.confirmBtn.isEnabled = (self.oldPwdTF.text!.lengthOfBytes(using: .utf8) > 0 && self.newPwdTF.text!.lengthOfBytes(using: .utf8) > 0) ? true : false
        self.confirmBtn.backgroundColor = self.confirmBtn.isEnabled ? rgb(r: 0x2f, g: 0x88, b: 0xff) : rgba(r: 0x2f, g: 0x88, b: 0xff, a: 0.4)
    }
    
    @IBAction func checkPwdSecurtyAction(_ sender: UIButton) {
        debugPrint("checkPwdSecurtyAction")
        sender.isSelected = !sender.isSelected
        if sender.tag == 100 {
            self.oldPwdTF.isSecureTextEntry = sender.isSelected ? false : true
        }else{
            self.newPwdTF.isSecureTextEntry = sender.isSelected ? false : true
        }
    }
    
    @IBAction func confirmBtnClickAction(_ sender: UIButton) {
        if self.newPwdTF.text!.lengthOfBytes(using: .utf8) >= 6 && self.newPwdTF.text!.lengthOfBytes(using: .utf8) <= 20{
            
            self.viewModel.nowPWD = self.newPwdTF.text! as NSString
            self.viewModel.oldPWD = self.oldPwdTF.text! as NSString
            
            self.viewModel.perform(self.viewModel.retSetPwdSEL)
            
        }else{
            debugPrint("请输入6~20位密码！")
        }
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UITextViewTextDidChange, object: nil)
    }
    
}


class DNReSetPWDViewModel: DNBaseViewModel {
    
    var oldPWD : NSString?
    var nowPWD : NSString?
    
    private(set) var retSetPwdSEL : Selector?
    
    override func initWithDelegate(delegate: AnyObject) -> Self {
        super.initWithDelegate(delegate: delegate)
        retSetPwdSEL = #selector(DNReSetPWDViewModel.resetPwdAction)
        return self
    }
    
    func resetPwdAction() -> Void {
        
        let api = DNApiForReSetPWD.init(newPwd: self.nowPWD!, oldPwd: self.oldPWD!)
        HTTPBinNetwork.request(api) { (response:SLResponse) in
            let model = DNCommonModel.deserialize(from: response.dataDictionary)
            debugPrint("response resetPwd info: \(model?.Message)")
        }
    }
    
}
